<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="预设名称" prop="fDriverWaitName">
        <el-input v-model="queryParams.fDriverWaitName" placeholder="请输入预设名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="添加日期">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['wf:wait:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="waitList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="fId" />
      <el-table-column label="预设编码" align="center" prop="fDriverWaitCode" />
      <el-table-column label="预设名称" align="center" prop="fDriverWaitName" />
      <el-table-column label="客户内码" align="center" prop="bcmClient.fClientCode" />
      <el-table-column label="产品名称" align="center" prop="bcmProduct.fProductName"  width="150"/>
      <el-table-column label="产品型号" align="center" prop="bcmProduct.fDeviceModel"  width="150"/>
      <el-table-column label="预设等待时间" align="center" prop="fDriverWaitTime"  width="120"/>
      <el-table-column label="状态" align="center" prop="fForbidStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.fForbidStatus" />
        </template>
      </el-table-column>
      <el-table-column label="添加人" align="center" prop="fCreaterName" width="80" />
      <el-table-column label="添加日期" align="center" prop="fCreateDate" width="180" />
      <el-table-column label="最后修改人" align="center" prop="fModifierName" />
      <el-table-column label="最后修改日期" align="center" prop="fModifyDate" width="180">
      </el-table-column>
      <el-table-column label="备注" align="center" prop="fRemark" />
      <el-table-column fixed="right" label="操作" align="center" min-width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row,1)"
          v-hasPermi="['system:post:edit']">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['wf:wait:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['wf:wait:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改驱动板等待时间管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="65%">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="预设编码" prop="fDriverWaitCode">
              <el-input v-model="form.fDriverWaitCode" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预设名称" prop="fDriverWaitName">
              <el-input v-model="form.fDriverWaitName" placeholder="请输入预设名称" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品" prop="fProductId">
              <div @click="openProduct" class="el-input-div">{{ product.fProductName }}</div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="产品型号" prop=" fDeviceModel">
              <el-input v-model="product.fDeviceModel"  :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="预设等待时间(秒)" prop="fDriverWaitTime">
              <el-input v-model="form.fDriverWaitTime" :disabled="isEdit == 1" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否启用" prop="fForbidStatus">
              <el-radio-group v-model="form.fForbidStatus" :disabled="isEdit == 1">
                <el-radio v-for="dict in fForbidStatus" :key="dict.value" :label="dict.value">{{ dict.label
                  }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
              <el-form-item label="客户内码" prop="fClientId">
              <el-select v-loadmore="clientLoad" v-model="form.fClientId" placeholder="请选择" :disabled="isEdit == 1">
                <el-option v-for="(item, index) in clientList" :key="index" :label="item.fClientCode"
                  :value="item.fId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="fRemark">
          <el-input v-model="form.fRemark" type="textarea" placeholder="请输入内容" :disabled="isEdit == 1" />
        </el-form-item><br>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="isEdit != 1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <addProt ref="product" @sure="sureProduct"></addProt>
  </div>
</template>

<script>
import { listWait, getWait, delWait, addWait, updateWait } from "@/api/basicData/wait";
import addProt from './components/addProt.vue';
import { listClient } from "@/api/basicData/customer"
export default {
  components: {
    addProt
  },
  name: "Wait",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      dialogFormVisible: false,
      isEdit: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 驱动板等待时间管理表格数据
      waitList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRange: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fDriverWaitCode: null,
        fDriverWaitName: null,
        fDeviceModel: undefined,
        fProductId: null,
        fDriverWaitTime: null,
        fCilentId: null,
        fRemark: null,
        fForbidStatus: null,
        fForbidDerName: null,
        fCreaterName: null,
        fCreateDate: null,
        fModifierName: null,
        fModifyDate: null,
        fDr: null
      },
      // 表单参数
      form: {
        fForbidStatus: 0
      },
      product: {},
      // 表单校验
      rules: {
        fProductName: [
          { required: true, message: "产品不能为空", trigger: "blur" }
        ],
        fDriverWaitName: [
          { required: true, message: "预设名称不能为空", trigger: "blur" }
        ],
        fDriverWaitTime: [
          { required: true, message: "预设等待时间不能为空", trigger: "blur" }
        ],
        fForbidStatus: [
          { required: true, message: "状态不能不选", trigger: "change" }
        ],

      },
      clientWhere: { pageNum: 1, pageSize: 20 },
      clientList: [],
      fForbidStatus: [
        { value: 0, label: '启用' },
        { value: 1, label: '停用' }
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {

    sureProduct(e){
      this.product = e
      this.form.fProductId = e.fId
      this.form.fProductName = e.fProductName
      this.form.fDeviceModel = e.fDeviceModel
      this.form.fProductSize = e.fProductSize
      this.form.fIcChip = e.fIcChip
      this.form.fModuleType = e.fModuleType
      this.form.fForbidStatus = e.fForbidStatus
    },
    openProduct() {
      this.$refs.product.open()
    },

    // 添加日期范围到查询参数
    addDateRange(params, dateRange) {
      params.fCreateDateStart = dateRange[0];
      params.fCreateDateEnd = dateRange[1];
      return params;
    },
    /** 查询驱动板等待时间管理列表 */
    getList() {
      this.loading = true;
      listWait(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.waitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fId: undefined,
        fDeviceModel: undefined,
        productSort: 0,
        status: "0",
        remark: undefined,
        fForbidStatus:0
      };
      this.resetForm("form");
      this.clientWhere.pageNum = 1
      this.clientList = []
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.isEdit = 0
      this.product={}
      this.reset();
      this.resetForm();
      this.getClient()
      this.open = true;
      this.title = "添加驱动板等待时间管理";
    },
    closeDialog() {
      this.clientWhere.pageNum = 1
      this.clientList = []
    },
    getClient() {
      listClient(this.clientWhere).then(res => {
        this.clientList = [...this.clientList, ...res.rows]
      })
    },
    clientLoad() {
      this.clientWhere.pageNum += 1
      this.getClient()
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      this.isEdit = type
      this.resetForm();
      this.getClient()
      this.reset();
      const fId = row.fId || this.ids
      getWait(fId).then(response => {
        this.form = response.data;
        this.product = response.data.bcmProduct
        this.form.fProductName = response.data.bcmProduct.fProductName;
        this.form.fDeviceModel = response.data.bcmProduct.fDeviceModel;
        this.open = true;
        if (type === 1) {
          this.open = true;
          this.title = "查看驱动板等待时间管理";
        } else {
          this.open = true;
          this.title = "修改驱动板等待时间管理";
        }              
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fId != null) {
            updateWait(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWait(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fIds = row.fId || this.ids;
      this.$modal.confirm('是否确认删除驱动板等待时间管理编号为"' + fIds + '"的数据项？').then(function () {
        return delWait(fIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wf/wait/export', {
        ...this.queryParams
      }, `wait_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
.el-input-div {
  width: 202px;
  height: 36px;
  box-sizing: border-box;
  padding: 0 15px;
  border: 1px solid #DCDFE6;
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap; // 强制一行显示
  overflow: hidden; // 超出隐藏
  text-overflow: ellipsis; // 省略号
}
</style>

